package org.itboys.mongodb.utils;

import com.google.common.collect.Lists;
import org.bson.types.ObjectId;

import java.util.ArrayList;
import java.util.List;

public class ObjectIdUtils {

	public static ObjectId[] toObjectIdArr(String[] ids) {
		if (ids == null || ids.length == 0) {
			return new ObjectId[0];
		}
		ObjectId[] arr = new ObjectId[ids.length];
		for (int i = 0; i < ids.length; i++) {
			arr[i] = new ObjectId(ids[i]);
		}
		return arr;
	}

	public static String[] toStringArr(ObjectId[] ids) {
		if (ids == null || ids.length == 0) {
			return new String[0];
		}
		String[] arr = new String[ids.length];
		for (int i = 0; i < ids.length; i++) {
			arr[i] = ids[i].toString();
		}
		return arr;
	}

	public static List<ObjectId> toObjectIdList(List<String> ids) {
		if (ids == null || ids.isEmpty()) {
			return new ArrayList<ObjectId>(0);
		}
		List<ObjectId> list = new ArrayList<>(ids.size());
		for (int i = 0, size = ids.size(); i < size; i++) {
			list.add(new ObjectId(ids.get(i)));
		}
		return list;
	}

	public static List<String> toStringList(List<ObjectId> ids) {
		if(ids == null) {
			return null;
		}
		List<String> idList = Lists.newArrayListWithCapacity(ids.size());
		for(ObjectId id : ids) {
			idList.add(id.toString());
		}
		return idList;
	}
}
